/***
*
* File: loginbox.nut
* Auth: Damian Wojdat
* Date: 20.10.2012
* Desc: Login panel.
*
*************************************************************************************************************/

local screen = guiGetScreenSize();
// local title = GUIFoint("bankgothic", 20);
local loginBox = {};
local spawnBox = {};

function onMakeLoginBox()
{
	/*
	* Login menu
	*/
	loginBox.window <- GUIWindow();
	loginBox.window.setSize(405.0, 275.0, false);
	loginBox.window.setPosition(screen[0]/2 - 405/2, screen[1]/2 -275/2, false);
	loginBox.window.setText("Login panel");
	loginBox.window.setProperty("CloseButtonEnabled", "false");
	loginBox.window.setVisible(true);

	loginBox.buttonLogin <- GUIButton();
	loginBox.buttonLogin.setText("Login");
	loginBox.buttonLogin.setSize(125.0, 30.0, false);
	loginBox.buttonLogin.setParent(loginBox.window.getName());
	loginBox.buttonLogin.setPosition(5.0, 215.0, false);
	loginBox.buttonLogin.setVisible(false);

	loginBox.buttonRegister <- GUIButton();
	loginBox.buttonRegister.setText("Register");
	loginBox.buttonRegister.setSize(125.0, 30.0, false);
	loginBox.buttonRegister.setParent(loginBox.window.getName());
	loginBox.buttonRegister.setPosition(138.0, 215.0, false);
	loginBox.buttonRegister.setVisible(false);

	loginBox.buttonExit <- GUIButton();
	loginBox.buttonExit.setText("Exit");
	loginBox.buttonExit.setSize(125.0, 30.0, false);
	loginBox.buttonExit.setParent(loginBox.window.getName());
	loginBox.buttonExit.setPosition(270.0, 215.0, false);
	loginBox.buttonExit.setVisible(false);

	loginBox.editBoxPasswd <- GUIEditBox();
	loginBox.editBoxPasswd.setSize(385.0, 20.0, false);
	loginBox.editBoxPasswd.setParent(loginBox.window.getName());
	loginBox.editBoxPasswd.setPosition(7.0, 185.0, false);
	loginBox.editBoxPasswd.setProperty("MaskText", "true");
	loginBox.editBoxPasswd.setVisible(false);

	loginBox.imageBanner <- GUIImage("sitelogo.jpg");
	loginBox.imageBanner.setSize(400.0, 75.0, false);
	loginBox.imageBanner.setParent(loginBox.window.getName());
	loginBox.imageBanner.setPosition(0.0, 0.0, false);
	loginBox.imageBanner.setVisible(false);

	loginBox.infoText <- GUIText();
	loginBox.infoText.setParent(loginBox.window.getName());
	loginBox.infoText.setSize(395.0, 10.0, false);
	loginBox.infoText.setPosition(4.0, 78.0, false);
	loginBox.infoText.setVisible(false);
	loginBox.infoText.setText("");
	
	loginBox.infoText2 <- GUIText();
	loginBox.infoText2.setParent(loginBox.window.getName());
	loginBox.infoText2.setSize(395.0, 10.0, false);
	loginBox.infoText2.setPosition(4.0, 88.0, false);
	loginBox.infoText2.setVisible(false);
	loginBox.infoText2.setText("");
	
	loginBox.infoText3 <- GUIText();
	loginBox.infoText3.setParent(loginBox.window.getName());
	loginBox.infoText3.setSize(395.0, 10.0, false);
	loginBox.infoText3.setPosition(4.0, 98.0, false);
	loginBox.infoText3.setVisible(false);
	loginBox.infoText3.setText("");

	/*
	* Spawn menu
	*/
	spawnBox.window <- GUIWindow();
	spawnBox.window.setSize(405.0, 318.0, false);
	spawnBox.window.setPosition(screen[0]/2 - 405/2, screen[1]/2 - 318/2, false);
	spawnBox.window.setText("Select faction (can be changed while playing)");
	spawnBox.window.setProperty("CloseButtonEnabled", "false");
	spawnBox.window.setVisible(false);

	loginBox.imageTeam1 <- GUIImage("factionpolice.jpg");
	loginBox.imageTeam1.setSize(390.0, 95.0, false);
	loginBox.imageTeam1.setParent(loginBox.window.getName());
	loginBox.imageTeam1.setPosition(0.0, 0.0, false);
	loginBox.imageTeam1.setVisible(false);

	loginBox.buttonTeam1 <- GUIButton();
	loginBox.buttonTeam1.setText("");
	loginBox.buttonTeam1.setSize(390.0, 95.0, false);
	loginBox.buttonTeam1.setParent(loginBox.window.getName());
	loginBox.buttonTeam1.setPosition(0.0, 0.0, false);
	loginBox.buttonTeam1.setVisible(false);
	loginBox.buttonTeam1.setAlpha(0.0);

	loginBox.imageTeam2 <- GUIImage("factiongang.jpg");
	loginBox.imageTeam2.setSize(390.0, 95.0, false);
	loginBox.imageTeam2.setParent(loginBox.window.getName());
	loginBox.imageTeam2.setPosition(0.0, 92.0, false);
	loginBox.imageTeam2.setVisible(false);

	loginBox.buttonTeam2 <- GUIButton();
	loginBox.buttonTeam2.setText("");
	loginBox.buttonTeam2.setSize(390.0, 95.0, false);
	loginBox.buttonTeam2.setParent(loginBox.window.getName());
	loginBox.buttonTeam2.setPosition(0.0, 92.0, false);
	loginBox.buttonTeam2.setVisible(false);
	loginBox.buttonTeam2.setAlpha(0.0);

	loginBox.buttonTeam3 <- GUIButton();
	loginBox.buttonTeam3.setText("");
	loginBox.buttonTeam3.setSize(390.0, 95.0, false);
	loginBox.buttonTeam3.setParent(loginBox.window.getName());
	loginBox.buttonTeam3.setPosition(0.0, 185.0, false);
	loginBox.buttonTeam3.setVisible(false);
	loginBox.buttonTeam3.setAlpha(0.0);

}
addEvent("makeLoginBox", onMakeLoginBox);

function onShowLoginBox(info = "", info2 = "", info3 = "")
{
	loginBox.window.setVisible(true);
	loginBox.buttonLogin.setVisible(true);
	loginBox.buttonRegister.setVisible(true);
	loginBox.buttonExit.setVisible(true);
	loginBox.editBoxPasswd.setVisible(true);
	loginBox.imageBanner.setVisible(true);
	loginBox.infoText.setText(info.tostring());
	loginBox.infoText2.setText(info2.tostring());
	loginBox.infoText3.setText(info3.tostring());
	loginBox.infoText.setVisible(true);
	loginBox.infoText2.setVisible(true);
	loginBox.infoText3.setVisible(true);
	guiToggleCursor(true);
}
addEvent("showLoginBox", onShowLoginBox);

function onShowSpawnBox()
{
	loginBox.window.setVisible(true);
	loginBox.buttonTeam1.setVisible(true);
	loginBox.imageTeam1.setVisible(true);
	loginBox.buttonTeam2.setVisible(true);
	loginBox.imageTeam2.setVisible(true);
	loginBox.buttonTeam3.setVisible(true);
	guiToggleCursor(true);
}
addEvent("showSpawnBox", onShowSpawnBox);

function onButtonClick(btnName, bState)
{
	if(btnName == loginBox.buttonLogin.getName())
	{
		triggerServerEvent("playerLogin", loginBox.editBoxPasswd.getText());
		loginBox.window.setVisible(false);
		loginBox.buttonLogin.setVisible(false);
		loginBox.buttonRegister.setVisible(false);
		loginBox.buttonExit.setVisible(false);
		loginBox.editBoxPasswd.setVisible(false);
		loginBox.editBoxPasswd.setText("");
		loginBox.imageBanner.setVisible(false);
		guiToggleCursor(false);
	}
	else if(btnName == loginBox.buttonRegister.getName())
	{
		triggerServerEvent("playerRegister", loginBox.editBoxPasswd.getText());
		loginBox.window.setVisible(false);
		loginBox.buttonLogin.setVisible(false);
		loginBox.buttonRegister.setVisible(false);
		loginBox.buttonExit.setVisible(false);
		loginBox.editBoxPasswd.setVisible(false);
		loginBox.editBoxPasswd.setText("");
		loginBox.imageBanner.setVisible(false);
		guiToggleCursor(false);
	}
	else if(btnName == loginBox.buttonTeam1.getName())
	{
		triggerServerEvent("playerFactionSelect", 0);
		loginBox.window.setVisible(false);
		loginBox.buttonTeam1.setVisible(false);
		loginBox.imageTeam1.setVisible(false);
		loginBox.buttonTeam2.setVisible(false);
		loginBox.imageTeam2.setVisible(false);
		loginBox.buttonTeam3.setVisible(false);
		guiToggleCursor(false);
	}
	else if(btnName == loginBox.buttonTeam2.getName())
	{
		triggerServerEvent("playerFactionSelect", 1);
		loginBox.window.setVisible(false);
		loginBox.buttonTeam1.setVisible(false);
		loginBox.imageTeam1.setVisible(false);
		loginBox.buttonTeam2.setVisible(false);
		loginBox.buttonTeam3.setVisible(false);
		guiToggleCursor(false);
	}
}
addEvent("buttonClick", onButtonClick);